//
//  XLUserSideEstimateVC.m
//  XiangLove
//
//  Created by 李传坤 on 2018/6/7.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLUserSideEstimateVC.h"
#import <ReactiveCocoa.h>
 
@interface XLUserSideEstimateVC ()
@property (weak, nonatomic) IBOutlet UILabel *timeAndJingyanLabel;
@property (weak, nonatomic) IBOutlet UIImageView *headerImage;
@property (strong, nonatomic)NSDictionary *callEndInfor;

@property (weak, nonatomic) IBOutlet UIButton *followButton;
@property (weak, nonatomic) IBOutlet UILabel *EstimateLabel;
@property (weak, nonatomic) IBOutlet UIButton *emoji1;
@property (weak, nonatomic) IBOutlet UIButton *emoji2;
@property (weak, nonatomic) IBOutlet UIButton *emoji3;
@property (weak, nonatomic) IBOutlet UIView *viewBiaoqian;//好标签bgview
@property (weak, nonatomic) IBOutlet UIView *badBiaoqianView;//中标签
@property (weak, nonatomic) IBOutlet UIView *badBadView;//差标签
@property (weak, nonatomic) IBOutlet UIButton *backbuton;
@property (strong, nonatomic)NSString *level;
@property (strong, nonatomic)NSString *CIDid;

@property (strong, nonatomic)NSMutableArray *GoodtittleArray;
@property (strong, nonatomic)NSMutableArray *GoodButtonArray;
@property (strong, nonatomic)NSMutableArray *GoodcidArr;
@property (strong, nonatomic)NSMutableArray *ChoiceGoodcidArr;

@property (strong, nonatomic)NSMutableArray *middletittleArray;
@property (strong, nonatomic)NSMutableArray *middleButtonArray;
@property (strong, nonatomic)NSMutableArray *middlecidArr;
@property (strong, nonatomic)NSMutableArray *ChoiceMiddlecidArr;

@property (strong, nonatomic)NSMutableArray *badtittleArray;
@property (strong, nonatomic)NSMutableArray *badButtonArray;
@property (strong, nonatomic)NSMutableArray *badcidArr;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (strong, nonatomic)NSMutableArray *ChoiceBadcidArr;
@end

@implementation XLUserSideEstimateVC

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    _headerImage.layer.cornerRadius = 55;
    _backbuton.layer.cornerRadius = 5;
    _backbuton.layer.borderWidth = 1;
    _backbuton.layer.borderColor = RGB_HEX(0xFFBB01).CGColor;
    
    _GoodtittleArray = [[NSMutableArray alloc]init];
    _GoodButtonArray = [[NSMutableArray alloc]init];
    _GoodcidArr = [[NSMutableArray alloc]init];
    _ChoiceGoodcidArr = [[NSMutableArray alloc]init];

    _middletittleArray = [[NSMutableArray alloc]init];
    _middleButtonArray = [[NSMutableArray alloc]init];
    _middlecidArr = [[NSMutableArray alloc]init];
    _ChoiceMiddlecidArr = [[NSMutableArray alloc]init];
    
    _badtittleArray = [[NSMutableArray alloc]init];
    _badButtonArray = [[NSMutableArray alloc]init];
    _badcidArr = [[NSMutableArray alloc]init];
    _ChoiceBadcidArr = [[NSMutableArray alloc]init];
    
    _level = @"2";
    [XLDownloadImageTool downloadImage:_headerImageUrl place:nil imageView:_headerImage];
    [self downloadCallVideoInfor];
    [self downloadEstimateInfor];
    // Do any additional setup after loading the view from its nib.
}
- (void)downloadCallVideoInfor{
    NSDictionary *dic = @{@"callId":_callid,@"userId":[[XLAccountManager shareInstance]model].Id,@"toUserId":_toUserId};
    [[[XLHTTPSessionManager sharedManager]XLV2GET:@"/api/v2/VideoChat/GetCallResult" parameters:dic resultClass:nil] subscribeNext:^(id x) {
        self.callEndInfor = x;
        NSString *minite = [[_timeString componentsSeparatedByString:@":"]objectAtIndex:1];
        NSString *second = [[_timeString componentsSeparatedByString:@":"]lastObject];
        if ([minite integerValue] > 0) {
            self.timeAndJingyanLabel.text = [NSString stringWithFormat:@"通话时长:%@分钟%@秒 经验值:+%@", minite, second, [self.callEndInfor objectForKey:@"EmpiricalValue"]];
            
            if(ValidStr([self.callEndInfor objectForKey:@"DisplayCard"]) && [[self.callEndInfor objectForKey:@"VideoIncome"] integerValue] > 0)
            {
                self.moneyLabel.text = [NSString stringWithFormat:@"消耗:%lu金豆,%@",[[self.callEndInfor objectForKey:@"VideoIncome"] integerValue] * 10,[self.callEndInfor objectForKey:@"DisplayCard"]];
            }else if(ValidStr([self.callEndInfor objectForKey:@"DisplayCard"]))
            {
                self.moneyLabel.text = [NSString stringWithFormat:@"消耗:%@",[self.callEndInfor objectForKey:@"DisplayCard"]];
            }else
            {
                self.moneyLabel.text = [NSString stringWithFormat:@"消耗:%lu金豆",[[self.callEndInfor objectForKey:@"VideoIncome"] integerValue] * 10];
            }
            
        }else{
            self.timeAndJingyanLabel.text = [NSString stringWithFormat:@"通话时长:%@秒 经验值:+%@", second, [self.callEndInfor objectForKey:@"EmpiricalValue"]];
            if(ValidStr([self.callEndInfor objectForKey:@"DisplayCard"]) && [[self.callEndInfor objectForKey:@"VideoIncome"] integerValue] > 0)
            {
                self.moneyLabel.text = [NSString stringWithFormat:@"消耗:%lu金豆,%@",[[self.callEndInfor objectForKey:@"VideoIncome"] integerValue] * 10,[self.callEndInfor objectForKey:@"DisplayCard"]];
            }else if(ValidStr([self.callEndInfor objectForKey:@"DisplayCard"]))
            {
                self.moneyLabel.text = [NSString stringWithFormat:@"消耗:%@",[self.callEndInfor objectForKey:@"DisplayCard"]];
            }else
            {
                self.moneyLabel.text = [NSString stringWithFormat:@"消耗:%lu金豆",[[self.callEndInfor objectForKey:@"VideoIncome"] integerValue] * 10];
            }
            
        }
//        self.timeAndJingyanLabel.text = [NSString stringWithFormat:@"通话时长:%@ 经验值:+%@", x[@"DisplayTime"], [self.callEndInfor objectForKey:@"EmpiricalValue"]];
        [XLDownloadImageTool downloadImage:self.callEndInfor[@"headImg"] place:nil imageView: _headerImage];

    } error:^(NSError *error) {
        
    }];
}

- (void)downloadEstimateInfor{
    [[[XLHTTPSessionManager sharedManager]XLV2GET:@"/api/v2/Comment/GetCommentLabels" parameters:@{@"level":_level} resultClass:nil] subscribeNext:^(id x) {
        if ([_level isEqualToString:@"1"]) {
            
            for (NSDictionary *dic in x) {
                [_middlecidArr addObject:dic[@"Id"]];
                [_middletittleArray addObject:dic[@"Name"]];
            }
            [self initBiaoqianMiddle];
        }else if ([_level isEqualToString:@"2"]){
            for (NSDictionary *dic in x) {
                [_GoodcidArr addObject:dic[@"Id"]];
                [_GoodtittleArray addObject:dic[@"Name"]];
            }
            [self initBiaoqianGood];
        }else{
            for (NSDictionary *dic in x) {
                [_badcidArr addObject:dic[@"Id"]];
                [_badtittleArray addObject:dic[@"Name"]];
            }
            [self initBiaoqianBad];
        }
        
    } error:^(NSError *error) {
        
    }];
}


#pragma mark ---添加关注
- (IBAction)followClickButton:(id)sender {
    
    [[[XLHTTPSessionManager sharedManager] XLV2POST:@"/api/v2/UserInfo/Follow" parameters:@{@"FollowId":self.toUserId,@"FansId":[XLAccountManager shareInstance].model.Id} resultClass:nil] subscribeNext:^(id x) {
        self.followButton.hidden = YES;
        [MBProgressHUD showContent:@"关注成功"];
        NSLog(@"关注成功%@",x);
    } error:^(NSError *error) {
        NSLog(@"关注失败%@",error);
        
    }];
    
}

- (IBAction)changeEstimate:(UIButton *)sender {
    [_emoji1 setImage:[UIImage imageNamed:@"video_icon_pj1_n"] forState:UIControlStateNormal];
    [_emoji2 setImage:[UIImage imageNamed:@"video_icon_pj2_n"] forState:UIControlStateNormal];
    [_emoji3 setImage:[UIImage imageNamed:@"video_icon_pj3_n"] forState:UIControlStateNormal];
    if (sender.tag == 1) {
        _viewBiaoqian.hidden = YES;_badBiaoqianView.hidden = YES;_badBadView.hidden = NO;
        _EstimateLabel.text = @"不满意";
        _level = @"0";
        if (_badtittleArray.count == 0) {
            [self downloadEstimateInfor];
        }
        [_emoji1 setImage:[UIImage imageNamed:@"video_icon_pj1_s"] forState:UIControlStateNormal];
    }
    if (sender.tag == 2) {
        _viewBiaoqian.hidden = YES;_badBiaoqianView.hidden = NO;_badBadView.hidden = YES;
        _EstimateLabel.text = @"一般";
        _level = @"1";
        if (_middletittleArray.count == 0) {
            [self downloadEstimateInfor];
        }
        [_emoji2 setImage:[UIImage imageNamed:@"video_icon_pj2_s"] forState:UIControlStateNormal];
    }
    if (sender.tag == 3) {
        _viewBiaoqian.hidden = NO;_badBiaoqianView.hidden = YES;_badBadView.hidden = YES;
        _EstimateLabel.text = @"非常满意";
        _level = @"2";
        [_emoji3 setImage:[UIImage imageNamed:@"video_icon_pj3_s"] forState:UIControlStateNormal];
    }
}


- (IBAction)commit:(id)sender {
    NSDictionary *dic;
    if ([_level isEqualToString:@"2"]) {
        NSString *arrayString = [self arrayToJSONString:_ChoiceGoodcidArr];
        dic = @{@"UserId"  :[[XLAccountManager shareInstance]model].Id,
                @"AnchorId":_toUserId,
                @"Level"   :_level,
                @"LabelIds":arrayString};
    }else if([_level isEqualToString:@"1"]) {
        NSString *arrayString = [self arrayToJSONString:_ChoiceMiddlecidArr];
        dic = @{@"UserId"  :[[XLAccountManager shareInstance]model].Id,
                @"AnchorId":_toUserId,
                @"Level"   :_level,
                @"LabelIds":arrayString};
    }else{
        NSString *arrayString = [self arrayToJSONString:_ChoiceMiddlecidArr];
        dic = @{@"UserId"  :[[XLAccountManager shareInstance]model].Id,
                @"AnchorId":_toUserId,
                @"Level"   :_level,
                @"LabelIds":arrayString};
    }
    
    [[[XLHTTPSessionManager sharedManager]XLV2POST:@"/api/v2/Comment/CommentAnchor" parameters:dic resultClass:nil]subscribeNext:^(id x) {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [MBProgressHUD showContent:@"评价成功" toView:window];
    } error:^(NSError *error) {
        
    }];
    UIViewController * presentingViewController = self.presentingViewController;
    while (presentingViewController.presentingViewController) {
        presentingViewController = presentingViewController.presentingViewController;
    }
    [presentingViewController dismissViewControllerAnimated:YES completion:nil];
}


- (NSString *)arrayToJSONString:(NSArray *)array{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}


- (IBAction)backFirstView:(id)sender {
    UIViewController * presentingViewController = self.presentingViewController;
    while (presentingViewController.presentingViewController) {
        presentingViewController = presentingViewController.presentingViewController;
    }
    [presentingViewController dismissViewControllerAnimated:YES completion:nil];
}


- (void)initBiaoqianGood{//好评标签初始化
    NSInteger wdith = ScreenWidth / 3 - 12;
    NSInteger height = 40;
    for (NSInteger i = 0 ; i < _GoodtittleArray.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.tag = 100 + i;
        button.layer.cornerRadius = 3;
        [button addTarget:self action:@selector(choceLanmu:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setTitle:_GoodtittleArray[i] forState:UIControlStateNormal];
        if (i < 3) {
            button.frame = CGRectMake(i * wdith+2, 0, wdith-4, 26);
            [_GoodButtonArray addObject:button];
            [_viewBiaoqian addSubview:button];
        }else if (i < 6){
            button.frame = CGRectMake((i-3) * wdith+2, height, wdith-4, 26);
            [_GoodButtonArray addObject:button];
            [_viewBiaoqian addSubview:button];
        }else{
            button.frame = CGRectMake((i-6) * wdith+2, height*2, wdith-4, 26);
            [_GoodButtonArray addObject:button];
            [_viewBiaoqian addSubview:button];
        }
    }
}
- (void)choceLanmu:(UIButton *)button{//好评标签选择
    _CIDid = _GoodcidArr[button.tag-100];
    NSInteger temp = [_CIDid integerValue];
    if (![_ChoiceGoodcidArr containsObject:_CIDid]) {
        [_ChoiceGoodcidArr addObject:@(temp)];
        button.layer.borderWidth = 1;
        button.layer.borderColor = RGB_HEX(0xFFBB01).CGColor;
        [button setTitleColor:RGB_HEX(0xFFBB01) forState:UIControlStateNormal];
    }else{
        [_ChoiceGoodcidArr removeObject:@(temp)];
        button.layer.borderWidth = 0;
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
}


- (void)initBiaoqianMiddle{//中评标签初始化
    NSInteger wdith = ScreenWidth / 3 - 12;
     NSInteger height = 40;
    for (NSInteger i = 0 ; i < _middletittleArray.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.tag = 100 + i;button.layer.cornerRadius = 3;
        [button addTarget:self action:@selector(choceLanmuMiddle:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setTitle:_middletittleArray[i] forState:UIControlStateNormal];
        if (i < 3) {
            button.frame = CGRectMake(i * wdith+2, 0, wdith-4, 26);
            [_middleButtonArray addObject:button];
            [_badBiaoqianView addSubview:button];
        }else if (i < 6){
            button.frame = CGRectMake((i-3) * wdith+2, height, wdith-4, 26);
            [_middleButtonArray addObject:button];
            [_badBiaoqianView addSubview:button];
        }else{
            button.frame = CGRectMake((i-6) * wdith+2, height*2, wdith-4, 26);
            [_middleButtonArray addObject:button];
            [_badBiaoqianView addSubview:button];
        }
    }
}
- (void)choceLanmuMiddle:(UIButton *)button{//中评标签选择
    _CIDid = _middlecidArr[button.tag-100];
    NSInteger temp = [_CIDid integerValue];
    if (![_ChoiceMiddlecidArr containsObject:_CIDid]) {
        [_ChoiceMiddlecidArr addObject:@(temp)];
        button.layer.borderWidth = 1;
        button.layer.borderColor = RGB_HEX(0xFFBB01).CGColor;
        [button setTitleColor:RGB_HEX(0xFFBB01) forState:UIControlStateNormal];
    }else{
        [_ChoiceMiddlecidArr removeObject:@(temp)];
        button.layer.borderWidth = 0;
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
}


- (void)initBiaoqianBad{//差评标签初始化
    NSInteger wdith = ScreenWidth / 3 - 12;
    NSInteger height = 40;
    for (NSInteger i = 0 ; i < _badtittleArray.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.tag = 100 + i;button.layer.cornerRadius = 3;
        [button addTarget:self action:@selector(choceLanmuBad:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setTitle:_badtittleArray[i] forState:UIControlStateNormal];
        if (i < 3) {
            button.frame = CGRectMake(i * wdith+2, 0, wdith-4, 26);
            [_badButtonArray addObject:button];
            [_badBadView addSubview:button];
        }else if (i < 6){
            button.frame = CGRectMake((i-3) * wdith+2, height, wdith-4, 26);
            [_badButtonArray addObject:button];
            [_badBadView addSubview:button];
        }else{
            button.frame = CGRectMake((i-6) * wdith+2, height*2, wdith-4, 26);
            [_badButtonArray addObject:button];
            [_badBadView addSubview:button];
        }
    }
}
- (void)choceLanmuBad:(UIButton *)button{//差评标签选择
    _CIDid = _badcidArr[button.tag-100];
    NSInteger temp = [_CIDid integerValue];
    if (![_ChoiceBadcidArr containsObject:_CIDid]) {
        [_ChoiceBadcidArr addObject:@(temp)];
        button.layer.borderWidth = 1;
        button.layer.borderColor = RGB_HEX(0xFFBB01).CGColor;
        [button setTitleColor:RGB_HEX(0xFFBB01) forState:UIControlStateNormal];
    }else{
        [_ChoiceBadcidArr removeObject:@(temp)];
        button.layer.borderWidth = 0;
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
